package com.cd.sendRequest.entity;
import lombok.Data;

import java.util.*;

/**
 * 通用 Curl 请求解析结果封装
 */
@Data
public class CurlRequest {
    private String url;
    private String method = "GET"; // 默认 GET
    private final Map<String, String> headers = new LinkedHashMap<>(); // 保持顺序
    private final List<Cookie> cookies = new ArrayList<>();
    private final Map<String, String> cookieMap = new LinkedHashMap<>();
    private String body;
    private String rawCommand; // 原始命令（可选）

    // --- Getters ---

    public String getUrl() { return url; }
    public void setUrl(String url) { this.url = url; }

    public String getMethod() { return method; }
    public void setMethod(String method) { this.method = method; }

    public Map<String, String> getHeaders() { return Collections.unmodifiableMap(headers); }
    public void addHeader(String key, String value) {
        headers.put(key, value);
        if (key.equalsIgnoreCase("Cookie")) {
            parseCookies(value);
        }
    }

    public List<Cookie> getCookies() { return Collections.unmodifiableList(cookies); }

    public String getBody() { return body; }
    public void setBody(String body) { this.body = body; }

    public String getRawCommand() { return rawCommand; }
    public void setRawCommand(String rawCommand) { this.rawCommand = rawCommand; }

    /**
     * 获取指定名称的 Cookie 值
     */
    public String getCookieValue(String name) {
        return cookies.stream()
                .filter(c -> c.getName().equals(name))
                .map(Cookie::getValue)
                .findFirst()
                .orElse(null);
    }

    @Override
    public String toString() {
        return "CurlRequest{" +
                "url='" + url + '\'' +
                ", method='" + method + '\'' +
                ", headers=" + headers +
                ", cookies=" + cookies +
                ", body='" + body + '\'' +
                '}';
    }

    // --- 内部类：Cookie ---
    public static class Cookie {
        private final String name;
        private final String value;

        public Cookie(String name, String value) {
            this.name = name;
            this.value = value;
        }

        public String getName() { return name; }
        public String getValue() { return value; }

        @Override
        public String toString() {
            return name + "=" + value;
        }
    }

    // --- 私有方法：解析 Cookie 字符串 ---
    private void parseCookies(String cookieHeader) {
        if (cookieHeader == null || cookieHeader.trim().isEmpty()) return;

        // 简单按分号拆分
        String[] pairs = cookieHeader.split(";");
        for (String pair : pairs) {
            String[] kv = pair.trim().split("=", 2);
            if (kv.length == 2) {
                cookies.add(new Cookie(kv[0], kv[1]));
            } else if (kv.length == 1 && !kv[0].isEmpty()) {
                // 处理无值的 cookie（如 secure, httponly）
                cookies.add(new Cookie(kv[0], ""));
            }
        }
    }
    public void addRawCookie(String cookieHeader) {
        if (cookieHeader == null || cookieHeader.trim().isEmpty()) return;
        String[] pairs = cookieHeader.split(";");
        for (String pair : pairs) {
            String[] kv = pair.trim().split("=", 2);
            if (kv.length == 2) {
                Cookie cookie = new Cookie(kv[0], kv[1]);
                cookies.add(cookie);
                cookieMap.put(kv[0], kv[1]); // 自动维护 map
            }
        }
    }

    public Map<String, String> getCookiesAsMap() {
        return Collections.unmodifiableMap(cookieMap);
    }
}